package fr.cie.blog.modeldao.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import fr.cie.blog.beans.Card;
import fr.cie.blog.beans.User;
import fr.cie.blog.domainservice.IServiceCard;
import fr.cie.blog.modeldao.services.GestionBaseDeDonnees;
import fr.cie.blog.modeldao.services.Mapper;

/**
 * @author AetM
 * 
 */
public class CardDaoImplSql extends ModelDAO implements IServiceCard {

	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger.getLogger(CardDaoImplSql.class);

	// ===================================================================== //
	// Sql Properties
	// ===================================================================== //

	/**
	 * mapper ORM Relation to Object.
	 */
	private final Mapper mapper = new Mapper();

	/**
	 * connection.
	 */
	private Connection connection = null;

	/**
	 * result.
	 */
	private ResultSet result = null;

	/**
	 * request.
	 */
	private PreparedStatement request = null;

	/**
	 * stringRequest.
	 */
	private String stringRequest = null;

	// ===================================================================== //
	// Properties business Object Card
	// ===================================================================== //

	/**
	 * card.
	 */
	private Card card;

	/**
	 * cardList.
	 */
	private List<Card> cardList;

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#save(java.lang.Object)
	 */
	@Override
	public final int save(final Card pCard) {
		Card cardBase = null;
		int resultDB = 0;

		try {

			connection = super.getConnection();
			stringRequest = "INSERT INTO card (UserId, StyleId, AvatarId, "
					+ "CountryID, Name, Address, ZipCode, City) "
					+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?);";

			request = connection.prepareStatement(stringRequest);
			request.setLong(1, pCard.getOwner().getId());
			request.setLong(2, pCard.getStyle().getId());
			request.setLong(3, pCard.getAvatar().getId());
			request.setLong(4, pCard.getCountry().getId());
			request.setString(5, pCard.getName());
			request.setString(6, pCard.getAddress());
			request.setString(7, pCard.getZipCode());
			request.setString(8, pCard.getCity());

			LOG.debug(request);
			// Execution de la requête
			resultDB = request.executeUpdate();
			

		} catch (final SQLException e) {
			LOG.error("createCard : base ou requete ou contrainte pb ?" + e);
		} finally {

			try {
				// Fermeture de la connexion
				if (result != null) {
					GestionBaseDeDonnees.getInstance().closeResulset(result);
				}
				if (request != null) {
					GestionBaseDeDonnees.getInstance().closeRequest(request);
				}
				if (connection != null) {
					GestionBaseDeDonnees.getInstance().closeConnection(
							connection);
				}
			} catch (final Exception ex) {
				LOG.debug("Erreur lors de la fermeture de la connexion avec "
						+ "la base de données dans la classe CardDaoImplSql "
						+ "fonction create" + ex);
			}

		}

		return resultDB;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#delete(java.lang.Object)
	 */
	@Override
	public final int delete(final Card pCard) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findAll()
	 */
	@Override
	public final List<Card> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findById(int)
	 */
	@Override
	public final Card findById(final long pIdentifier) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.cie.blog.domainservice.IServiceCard#findByUser(fr.cie.blog.beans.User)
	 */
	@Override
	public final Card findByUser(final User pUser) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#update(java.lang.Object)
	 */
	@Override
	public final int update(final Card pCard) {
		// TODO Auto-generated method stub
		return 0;
	}

}